package com.dsl.dslsuzhou.controller;

import com.dsl.dslsuzhou.common.Result;
import com.dsl.dslsuzhou.engine.BuiltSql;
import com.dsl.dslsuzhou.mapper.JdbcExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试数据控制器
 */
@RestController
@RequestMapping("/api/${api-module}/test-data")
@CrossOrigin(origins = "*")
public class TestDataController {
    
    private static final Logger logger = LoggerFactory.getLogger(TestDataController.class);
    
    @Autowired
    private JdbcExecutor jdbcExecutor;

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    /**
     * 插入今日占比分析测试数据
     */
    @PostMapping("/insert-today-proportion")
    public Result<String> insertTodayProportionData() {
        try {
            logger.info("开始插入今日占比分析测试数据");
            
            String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            
            // 清理今天的测试数据
            String deleteSql = "DELETE FROM fact_device_energy WHERE DATE(date) = ?";
            jdbcTemplate.update(deleteSql, today);
            
            // 插入测试数据
            String insertSql = "INSERT INTO fact_device_energy (floor_id, subsystem_id, energy_value, date) VALUES (?, ?, ?, ?)";
            
            List<Object[]> batchData = new ArrayList<>();
            
            // A区1栋各楼层数据（用于场景2测试）
            // A区1栋1楼 (floor_id=1)
            batchData.add(new Object[]{1, 4, 150.50, today + " 08:00:00"});
            batchData.add(new Object[]{1, 4, 165.75, today + " 09:00:00"});
            batchData.add(new Object[]{1, 4, 180.25, today + " 10:00:00"});
            batchData.add(new Object[]{1, 4, 145.80, today + " 11:00:00"});
            batchData.add(new Object[]{1, 4, 170.90, today + " 12:00:00"});
            batchData.add(new Object[]{1, 4, 155.60, today + " 13:00:00"});
            batchData.add(new Object[]{1, 4, 175.40, today + " 14:00:00"});
            batchData.add(new Object[]{1, 4, 160.30, today + " 15:00:00"});
            
            // A区1栋2楼 (floor_id=2)
            batchData.add(new Object[]{2, 4, 120.30, today + " 08:00:00"});
            batchData.add(new Object[]{2, 4, 135.45, today + " 09:00:00"});
            batchData.add(new Object[]{2, 4, 140.60, today + " 10:00:00"});
            batchData.add(new Object[]{2, 4, 125.75, today + " 11:00:00"});
            batchData.add(new Object[]{2, 4, 150.90, today + " 12:00:00"});
            batchData.add(new Object[]{2, 4, 145.20, today + " 13:00:00"});
            batchData.add(new Object[]{2, 4, 155.35, today + " 14:00:00"});
            batchData.add(new Object[]{2, 4, 130.50, today + " 15:00:00"});
            
            // A区1栋3楼 (floor_id=3)
            batchData.add(new Object[]{3, 4, 110.20, today + " 08:00:00"});
            batchData.add(new Object[]{3, 4, 125.35, today + " 09:00:00"});
            batchData.add(new Object[]{3, 4, 130.50, today + " 10:00:00"});
            batchData.add(new Object[]{3, 4, 115.65, today + " 11:00:00"});
            batchData.add(new Object[]{3, 4, 140.80, today + " 12:00:00"});
            batchData.add(new Object[]{3, 4, 135.95, today + " 13:00:00"});
            batchData.add(new Object[]{3, 4, 145.10, today + " 14:00:00"});
            batchData.add(new Object[]{3, 4, 120.25, today + " 15:00:00"});
            
            // A区2栋各楼层数据（用于场景1测试）
            // A区2栋1楼 (floor_id=4)
            batchData.add(new Object[]{4, 4, 200.50, today + " 08:00:00"});
            batchData.add(new Object[]{4, 4, 215.75, today + " 09:00:00"});
            batchData.add(new Object[]{4, 4, 230.25, today + " 10:00:00"});
            batchData.add(new Object[]{4, 4, 195.80, today + " 11:00:00"});
            batchData.add(new Object[]{4, 4, 220.90, today + " 12:00:00"});
            batchData.add(new Object[]{4, 4, 205.60, today + " 13:00:00"});
            batchData.add(new Object[]{4, 4, 225.40, today + " 14:00:00"});
            batchData.add(new Object[]{4, 4, 210.30, today + " 15:00:00"});
            
            // A区2栋2楼 (floor_id=5)
            batchData.add(new Object[]{5, 4, 180.30, today + " 08:00:00"});
            batchData.add(new Object[]{5, 4, 195.45, today + " 09:00:00"});
            batchData.add(new Object[]{5, 4, 200.60, today + " 10:00:00"});
            batchData.add(new Object[]{5, 4, 185.75, today + " 11:00:00"});
            batchData.add(new Object[]{5, 4, 210.90, today + " 12:00:00"});
            batchData.add(new Object[]{5, 4, 205.20, today + " 13:00:00"});
            batchData.add(new Object[]{5, 4, 215.35, today + " 14:00:00"});
            batchData.add(new Object[]{5, 4, 190.50, today + " 15:00:00"});
            
            // B区1栋各楼层数据（用于场景1测试）
            // B区1栋1楼 (floor_id=6)
            batchData.add(new Object[]{6, 4, 160.40, today + " 08:00:00"});
            batchData.add(new Object[]{6, 4, 175.55, today + " 09:00:00"});
            batchData.add(new Object[]{6, 4, 180.70, today + " 10:00:00"});
            batchData.add(new Object[]{6, 4, 165.85, today + " 11:00:00"});
            batchData.add(new Object[]{6, 4, 190.00, today + " 12:00:00"});
            batchData.add(new Object[]{6, 4, 185.30, today + " 13:00:00"});
            batchData.add(new Object[]{6, 4, 195.45, today + " 14:00:00"});
            batchData.add(new Object[]{6, 4, 170.60, today + " 15:00:00"});
            
            // B区1栋2楼 (floor_id=7)
            batchData.add(new Object[]{7, 4, 140.25, today + " 08:00:00"});
            batchData.add(new Object[]{7, 4, 155.40, today + " 09:00:00"});
            batchData.add(new Object[]{7, 4, 160.55, today + " 10:00:00"});
            batchData.add(new Object[]{7, 4, 145.70, today + " 11:00:00"});
            batchData.add(new Object[]{7, 4, 170.85, today + " 12:00:00"});
            batchData.add(new Object[]{7, 4, 165.15, today + " 13:00:00"});
            batchData.add(new Object[]{7, 4, 175.30, today + " 14:00:00"});
            batchData.add(new Object[]{7, 4, 150.45, today + " 15:00:00"});
            
            // 批量插入数据
            for (Object[] data : batchData) {
                jdbcTemplate.update(insertSql, data);
            }
            
            logger.info("成功插入{}条今日测试数据", batchData.size());
            
            return Result.success("成功插入" + batchData.size() + "条今日占比分析测试数据");
            
        } catch (Exception e) {
            logger.error("插入今日测试数据失败", e);
            return Result.error("插入测试数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询今日数据统计
     */
    @GetMapping("/today-stats")
    public Result<Map<String, Object>> getTodayStats() {
        try {
            String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            
            String sql = "SELECT " +
                        "COUNT(*) as total_records, " +
                        "COUNT(DISTINCT floor_id) as unique_floors, " +
                        "ROUND(SUM(energy_value), 2) as total_energy, " +
                        "MIN(date) as earliest_time, " +
                        "MAX(date) as latest_time " +
                        "FROM fact_device_energy " +
                        "WHERE DATE(date) = ? AND subsystem_id = 4";
            
            BuiltSql builtSql = new BuiltSql(sql, java.util.Arrays.asList(today));
            List<Map<String, Object>> results = jdbcExecutor.executeQuery(builtSql);
            
            if (!results.isEmpty()) {
                return Result.success(results.get(0));
            } else {
                return Result.success(new HashMap<>());
            }
            
        } catch (Exception e) {
            logger.error("查询今日数据统计失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}
